package com.ssy.lingxi.product.serviceImpl.feign;

import com.ssy.lingxi.common.response.ResponseCode;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.member.merchant.api.feign.inner.MemberInnerControllerFeign;
import com.ssy.lingxi.member.merchant.api.model.vo.inner.request.MemberFeignVO;
import com.ssy.lingxi.member.merchant.api.model.vo.inner.response.MemberFeignPageQueryVO;
import com.ssy.lingxi.product.model.dto.platform.PlatformMaterialProcessMemberDTO;
import com.ssy.lingxi.product.service.feign.IMemberFeignService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 彭冬冬
 * @version 2.0.0
 * @date 2022/3/28 10:56
 */
@Service
public class MemberFeignServiceImpl implements IMemberFeignService {

    @Resource
    private MemberInnerControllerFeign memberInnerControllerFeign;

    @Override
    public Wrapper<List<PlatformMaterialProcessMemberDTO>> findPlatformMembers(String name, List<MemberFeignVO> members) {
        Wrapper<List<MemberFeignPageQueryVO>> feignResult = memberInnerControllerFeign.findPlatformMembers(members);
        if (feignResult.getCode() != ResponseCode.SUCCESS.getCode()) {
            return Wrapper.fail(feignResult.getCode(), feignResult.getMessage());
        }

        if (CollectionUtils.isEmpty(feignResult.getData())) {
            return Wrapper.success(new ArrayList<>());
        }
        return Wrapper.success(feignResult.getData().stream().map(map -> {
            PlatformMaterialProcessMemberDTO target = new PlatformMaterialProcessMemberDTO();
            target.setMemberId(map.getMemberId());
            target.setRoleId(map.getRoleId());
            target.setName(map.getName());
            target.setRoleName(map.getRoleName());
            target.setMemberTypeName(map.getMemberTypeName());
            target.setLevel(map.getLevel());
            target.setLevelTag(map.getLevelTag());
            return target;
        }).collect(Collectors.toList()).stream().filter(f -> {
            if (StringUtils.hasLength(name)) {
                return f.getName().contains(name.trim());
            } else {
                return true;
            }
        }).sorted(Comparator.comparingLong(PlatformMaterialProcessMemberDTO::getMemberId).thenComparingLong(PlatformMaterialProcessMemberDTO::getRoleId)).collect(Collectors.toList()));
    }
}
